package client.view;

import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.Collection;

import javax.swing.AbstractAction;

import client.Client;

import commands.ChangeVisibilityCommand;

import core.Card;
import core.Visibility;
import core.Zone;

/**
 * Represents the action of setting the visibility of a Card or a Zone.
 */
public class SetVisibilityAction extends AbstractAction
{
	private final Visibility visibility;
	private final Zone zone;
	private final Card card;
	private final Client client;
	
	/**
	 * Create an action to represent the setting of the visibility of each card
	 * registered in the SelectionRegistry
	 * @param caption the caption for the action (to appear in menus, buttons, etc.)
	 * @param v the Visibility to apply to each card in the zone
	 * @param cl the Client taking this action
	 */
	public SetVisibilityAction(String caption, Visibility v, Card c, Client cl)
	{
		super(caption);
		client = cl;
		card = c;
		visibility = v;
		zone = null;
	}
	
	/**
	 * Create an action to represent the setting of the visibility of all the cards
	 * in a zone
	 * @param caption the caption for the action (to appear in menus, buttons, etc.)
	 * @param v the Visibility to apply to each card in the zone
	 * @param z the Zone to affect
	 * @param cl the Client taking this action
	 */
	public SetVisibilityAction(String caption, Visibility v, Zone z, Client cl)
	{
		super(caption);
		client = cl;
		card = null;
		visibility = v;
		zone = z;

	}
	
	
	/**
	 * If the SetVisibilityAction was created for a Zone, then the visibility of each
	 * card in the zone is set; otherwise the visibility for the given card is set.
	 */
	public void actionPerformed(ActionEvent ae) 
	{
		Collection<Card> cards = null;
		
		SelectionRegistry.add(card);
		
		if (zone != null)
			cards = zone.getCards();
		else 
			cards = SelectionRegistry.getSelectedCards();
		
		
		try {
			for (Card card : new java.util.LinkedList<Card>(cards))
				client.sendCommand(new ChangeVisibilityCommand(card, visibility));
		} catch (IOException e) {e.printStackTrace();}
		SelectionRegistry.clear();
	}
}
